$(function () {
    $("#jqGrid").Grid({
        url: '../otctradeorder/list?enable=1',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '订单编号', name: 'tradeordersn', index: 'tradeordersn', width: 80,align:'center'},
			{label: '订单状态', name: 'statusname', index: 'statusname', width: 80,align:'center'},
			{label: '单价', name: 'untilprice', index: 'untilprice', width: 80,align:'center'},
			{label: '数量', name: 'number', index: 'number', width: 80,align:'center'},
			{label: '交易金额', name: 'totalmoney', index: 'totalmoney', width: 80,align:'center'},
			{label: '手续费率', name: 'feerate', index: 'feerate', width: 80,align:'center'},
			{label: '付款方式', name: 'paymodel', index: 'paymodel',align:'center', width: 80,
				formatter:function (value) {
					if(value==0){
						return "银行卡";
					}else if(value==1){
						return	"支付宝";
					}else if(value==2){
						return	"微信";
					}else {
						return "";
					}
				}
			},
			{label: '付款期限', name: 'paylimit', index: 'paylimit', width: 80,align:'center'},
			{label: '溢价比率', name: 'premiumrate', index: 'premiumrate', width: 80,align:'center'},
			{label: '订单类型', name: 'type', index: 'type',align:'center', width: 80,
                formatter: function (value) {
                    if(value == 0)
                        return "购买订单";
                    else if(value == 1)
                        return "出售订单";
                }},
			{label: '购买人', name: 'buyusername', index: 'buyusername',align:'center', width: 80},
            {label: '购买人手机', name: 'buyphone', index: 'buyphone',align:'center', width: 80},
			{label: '发布人', name: 'sellerusername', index: 'sellerusername',align:'center', width: 80},
            {label: '发布人手机', name: 'sellerphone', index: 'sellerphone',align:'center', width: 80},
			{label: '参考号', name: 'referencenum', index: 'referencenum',align:'center', width: 80}


			/*{label: '创建日期', name: 'createdat', index: 'createdAt', width: 80,align:'center', formatter: function (value) {
                    return transDate(value);
                }},
			{label: '更新日期', name: 'updatedat', index: 'updatedAt',align:'center', width: 80, formatter: function (value) {
                    return transDate(value);
                }}*/
                ]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		otcTradeorder: {},
		falseOtcTradeorder:{},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: '', qstatus: '', qtype: '',phone: '',datetime:'',starttime:'',endtime:''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.otcTradeorder = {};
			vm.queryFalseOrderModel();//假单模板
		},
		update: function (event) {
			let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
			vm.title = "修改";

			vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			let url = vm.otcTradeorder.id == null ? "../otctradeorder/save" : "../otctradeorder/update";
			Ajax.request({
				url: url,
				params: JSON.stringify(vm.otcTradeorder),
				type: "POST",
				contentType: "application/json",
				successCallback: function (r) {
					alert('操作成功', function (index) {
						vm.reload();
					});
				}
			});
		},
		del: function (event) {
			let ids = getSelectedRows("#jqGrid");
			if (ids == null) {
				return;
			}

			confirm('确定要删除选中的记录？', function () {
				Ajax.request({
					url: "../otctradeorder/delete",
					params: JSON.stringify(ids),
					type: "POST",
					contentType: "application/json",
					successCallback: function () {
						alert('操作成功', function (index) {
							vm.reload();
						});
					}
				});
			});
		},
		getInfo: function (id) {

			Ajax.request({
				url: "../otctradeorder/info/" + id,
				async: true,
				successCallback: function (r) {
					vm.otcTradeorder = r.otcTradeorder;
				}
			});

		},
		reload: function (event) {
			vm.showList = true;
			let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			if(vm.q.datetime != null){
				vm.q.starttime = vm.changeDate(vm.q.datetime[0]);
				vm.q.endtime = vm.changeDate(vm.q.datetime[1]);
			}
			$("#jqGrid").jqGrid('setGridParam', {
				postData: {'name': vm.q.name,'qtype': vm.q.qtype,'qstatus': vm.q.qstatus,'phone': vm.q.phone,'starttime':vm.q.starttime,'endtime':vm.q.endtime},
				page: page
			}).trigger("reloadGrid");
			vm.handleReset('formValidate');
		},
		reloadSearch: function () {
			vm.q = {
                name: '', qstatus: '', qtype: '',phone: '',datetime:'',starttime:'',endtime:''
			}
			vm.reload();
		},
		handleSubmit: function (name) {
			handleSubmitValidate(this, name, function () {
				vm.saveOrUpdate()
			});
		},
		handleReset: function (name) {
			handleResetForm(this, name);
		},
		queryFalseOrderModel: function () {//加单模板
			Ajax.request({
				url: "../otctradeorder/queryModel",
				async: true,
				successCallback: function (r) {
					vm.otcTradeorder = r.otcTradeorder;
				}
			});
		},
		saveFlaseOrderModel:function () {//添加假单
			// let url = vm.otcTradeorder.id == null ? "" : "../otctradeorder/update";
			if(vm.otcTradeorder.id!= null){
				alert("用户订单无法提交!")
				return;
			}
			if(vm.otcTradeorder.number==null){
				alert("请输入订单数量!");
				return;
			}
			Ajax.request({
				url: "../otctradeorder/save",
				params: JSON.stringify(vm.otcTradeorder),
				type: "POST",
				contentType: "application/json",
				successCallback: function (r) {
					alert('操作成功', function (index) {
						vm.reload();
					});
				}
			});
		},changeDate:function (str) {
			var dateee = new Date(str).toJSON();
			var date = new Date(+new Date(dateee)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'');
			return date;
		},exportExcel:function(event){
			let ids = getSelectedRows("#jqGrid");
			if (ids == null) {
				return;
			}
			var exportUrl = "../otctradeorder/exportExcel?ids="+ids;
			location.href = exportUrl;
		}
	}
});